$(document).ready(function(){

	var colors = new Array(
    'blue',
    'green',
    'yellow',
    'orange2',
    'purple',
    'orange',
    'deepblue'
  );
	var addcolors = new Array(
    '#0c84b0',
    '#5ab39d',
    '#fdc32b',
    '#272822',
    '#3f4564',
    '#f69448',
    '#336699'
  );
	$('#login_sub').bind('submit',function(event){
		event.preventDefault();
		var un = $('#user_name').val();
		var psw = $('#user_pw').val();
			$.ajax({
			'type':'POST',
			'url':'s1_json.php',
			'data':'login=1&un=' + un + '&psw=' + psw,
			'dataType':'json',
			'success':function(data){
				var value = new Array();cnt = 0;
				$.each(data,function(index,val){
					value[index] = val;
					console.log(value[index]);
					console.log(index);
					console.log(cnt++);
				});
				if(value['user_name'] != null)
				{
					$.cookie('user_name',value['user_name']);
					console.log(value['user_name']);
					console.log($.cookie('user_name'));
					$('button.close').trigger('click');
				}
				else
				{
					$('.modal-body').append('<span class="error">用户名或密码输入错误！</span>');
				}
			}
		});

	});
	/*$.*/
	var doc = new Array();
	var s1_top = ($(window).height() - $('#s1_right').height())/2;
	$('#s1_right').css({'top':s1_top});//调整侧边栏位置为屏幕的右侧中间
	$.ajax({
		"type":"GET",
		"url":"s1_json.php",
		'dataType':'json',
		'data':'type=content',
		"success":function(data){//加载json
			var max;	var right_html='';
			console.log(data);
			$.each(data,function(entryIndex,entry){
				doc[entryIndex] = (
					[entry.wall_first,entry.wall_sec,entry.wall_author,entry.wall_id]//遍历json
					);
				right_html += '<tr><td><ul>';
				right_html += '<li class="sentence">' + doc[entryIndex][0] + '</li>';
				right_html += '<li class="sentence">' +  doc[entryIndex][1] + '</li>';
				right_html += '<li class="author">' +  doc[entryIndex][2] + 
				'<input class="right_checkbox" type="checkbox" name="delete[]" value="'+ doc[entryIndex][3] + '">' + '</li>';
				right_html += '</ul></td></tr>';
				max = entryIndex;//记录最大的index防止切换文字时溢出
			});
			$('#right_contain').append(right_html);//添加到侧边栏
		$.getScript('../js/scrollbar.js');//添加过侧边栏数据之后再加载js否则会失效
		$('.s1_submit,.right_checkbox').hide();
		max++;
		$('#f1').text(doc[0][0]);
		$('#f2').text(doc[0][1]);
		$('#author').text(doc[0][2]);//修改首页的文字

		var cnt=0;//用来计数
		function change(){//切换背景颜色和主页文字
			cnt++;
			$('#f1,#f2,#author').fadeOut('fast',function(){
				$('#f1').text(doc[cnt%max][0]);
				$('#f2').text(doc[cnt%max][1]);
				$('#author').text(doc[cnt%max][2]);
			}).fadeIn('slow');//文字切换
			
			$('body').animate({backgroundColor: addcolors[(cnt-1)%7]});//背景颜色切换
		};
		var changeColor = setInterval(change,3000);
		$('#s1_right').animate({
			right: '-335px'},{	
				duration:'slow',
				specialEasing:{
					right:'easeInQuart'
				}
			}).addClass('closed');
		$('.closed').click(function(){
				//$('body,.s1').stop();
				clearInterval(changeColor);
				$('.closed').animate({
					right: '0px'},{	
						duration:'slow',
						specialEasing:{
							right:'easeInQuart'
					}
				}).removeClass('closed');
			});
		$('#right_back').click(function(){//让侧边栏缩移动到左边隐藏起来。
			$('#s1_right').animate({
				right: '-335px'},{	
					duration:'slow',
					specialEasing:{
						right:'easeInQuart'
				}
			}).addClass('closed');
			changeColor = setInterval(change,3000);
			return false;
		});
		$('#setting').click(function(){
			var log = $.cookie('user_name');
			console.log(log);
			if(log != null)
				$('.s1_submit,.right_checkbox').toggle('fast');
			else
			{
				$('#s1_login').trigger('click');
				var log_top =($(window).height() - 365)/2;
				$('.modal-dialog').css({'top':log_top});//调整登录窗口位置
			}
		});
		}//加载json结束
	});
	
	$('#form').hide().fadeIn(3000);
	
	$('#test').click(function(){
		$('.head').animate({backgroundColor: '#f00'},1000);
	});

	var i=1;
	$('.switch-li').click(function(){

		$('#row2').slideToggle('slow');
		$('#row1').slideToggle('slow');
		i++;
		if(i%2 == 0)
			$('#gl-row').removeClass('glyphicon-circle-arrow-up').addClass('glyphicon-circle-arrow-down');
		else
			$('#gl-row').removeClass('glyphicon-circle-arrow-down').addClass('glyphicon-circle-arrow-up');
	});

	$('#li1,#li2,#li3,#contact').mouseover(function(){
		$(this).tooltip('show');
	});

	$('#head li').click(function(){
		$('#head li').removeClass('active');
		$(this).addClass('active');
	});

	$('#close1').click(function(){
		$('#head li').removeClass('active');
		$('#li1').addClass('active');
	});

	$("#submit").click(function(e){
		$input = $(this).parents('form').find('input[type="text"]');
		var flag = 0;
		for(var i=0;i<3;i++){
			if( $input.eq(i).val().length < 1)
			{

				$input.eq(i).addClass('has-error');
				$(this).parents('form').find('th').eq(1).text('请不要输入空值哦');
				flag = 1;
			}
		}
		if(flag)
			return false;
	});
})